package get.impl;


import domain.Page;
import get.HtmlGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class DefaultHtmlGetter implements HtmlGet {

    @Override
    public Page get(String url){
        Page page = new Page();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpUriRequest httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(httpget);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (response.getStatusLine().getStatusCode() == 200) {
            try {
                page.setContent(EntityUtils.toString(response.getEntity(), "UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            page.setUrl(url);
            return page;
        }
        return null;
    }
}
